import sqlite3, sys, os

# usage: add-team.py team_name team_sport_type_id team_city team_country [team_syn1 team_syn2 ...]

def ConnectToDB(dbName):
    db = sqlite3.connect(dbName)
    
    return db
    
def AddTeam():    
    conn.execute("insert into teams (Name, SportTypeID, City, Country) values (?, ?, ?, ?)", (sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]))
    conn.execute("select last_insert_rowid()")
    return conn.fetchone()[0]
    
def AddSynonyms(teamID):
    for i in range(0, len(sys.argv) - 4 - 1):
        conn.execute("insert into teamsynonyms (TeamID, Synonym) values (?, ?)", (teamID, sys.argv[i + 5]))
    
    

dbName = "teams.db"
if (not os.path.exists(dbName)):
    print("Не найден файл базы данных, проверьте его доступность")
    sys.exit(1)
    
if (len(sys.argv) < 5):
    print ("использование: add-team.py team_name team_sport_type_id team_city team_country [team_syn1 team_syn2 ...]")
    sys.exit(1)
        
db = ConnectToDB(dbName)
conn = db.cursor()

sys.argv = ("\"" + "\" \"".join(sys.argv) + "\"")[1:-1].split("\" \"")

teamID = AddTeam()
AddSynonyms(teamID)

db.commit()
db.close()